package com.msh.starter.common.base;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.msh.frame.client.base.BasePo;
import com.msh.frame.client.base.BaseQo;
import com.msh.frame.client.common.Result;
import java.util.List;

/**
 * @author shihu 带缓存的serviceImpl 缓存单条信息和list信息，分页信息都缓存 list缓存有时效，如果需要实时信息就不要使用此类
 */
public abstract class BaseAllCacheServiceImpl<T extends BasePo, Q extends BaseQo> extends
    BaseCacheServiceImpl<T, Q> implements IClearListCache<Q> {

  /**
   * 列表缓存
   */
  final private static String LIST_PREFIX = "list:";
  /**
   * 获取列表查询单条信息缓存
   */
  final private static String LIST_ONE_PREFIX = "list_one:";
  /**
   * 查询条数缓存
   */
  final private static String COUNT_PREFIX = "count:";
  /**
   * list查询结果缓存时间 可以根据业务自己设置 默认为3分钟
   */
  private long expireSecondTimeForList = 60L * 3L;

  @Override
  public List<T> list(Q param) {
    String key = LIST_PREFIX + JSON.toJSONString(param);
    List<T> list = (List<T>) getCache().get(key);
    if (null == list) {
      list = super.list(param);
      getCache().put(key, list, expireSecondTimeForList);
    }
    return list;
  }

  @Override
  public Result<List<T>> page(Q param) {
    String key = LIST_PREFIX + JSON.toJSONString(param);
    Result<List<T>> result = (Result<List<T>>) getCache().get(key);
    if (null == result) {
      result = super.page(param);
      if (0 == result.getCode()) {
        getCache().put(key, result, expireSecondTimeForList);
        String paramStr = JSON.toJSONString(param);
        JSONObject jsonObject = JSON.parseObject(paramStr);
        jsonObject.remove("firstRow");
        jsonObject.remove("currentPage");
        jsonObject.remove("pageSize");
        String countKey = COUNT_PREFIX + jsonObject.toJSONString();
        getCache().put(countKey, result.getCount(), expireSecondTimeForList);
      }
    }
    return result;
  }

  @Override
  public T listOne(Q param) {
    String key = LIST_ONE_PREFIX + JSON.toJSONString(param);
    T t = (T) getCache().get(key);
    if (null == t) {
      t = super.listOne(param);
      getCache().put(key, t, expireSecondTimeForList);
    }
    return t;
  }

  @Override
  public int count(Q param) {
    String paramStr = JSON.toJSONString(param);
    JSONObject jsonObject = JSON.parseObject(paramStr);
    jsonObject.remove("firstRow");
    jsonObject.remove("currentPage");
    jsonObject.remove("pageSize");
    String key = COUNT_PREFIX + jsonObject.toJSONString();
    Integer count = (Integer) getCache().get(key);
    if (null == count) {
      count = super.count(param);
      getCache().put(key, count, expireSecondTimeForList);
    }
    return count;
  }

  public void setExpireSecondTimeForList(long expireSecondTimeForList) {
    this.expireSecondTimeForList = expireSecondTimeForList;
  }

  @Override
  public void clearListCache(Q baseQo) {
    getCache().remove(LIST_PREFIX + baseQo.toString());
    String paramStr = JSON.toJSONString(baseQo);
    JSONObject jsonObject = JSON.parseObject(paramStr);
    jsonObject.remove("firstRow");
    jsonObject.remove("currentPage");
    jsonObject.remove("pageSize");
    String countKey = COUNT_PREFIX + jsonObject.toJSONString();
    getCache().remove(COUNT_PREFIX + baseQo.toString());
  }

  @Override
  public void clearListOneCache(Q baseQo) {
    String paramStr = JSON.toJSONString(baseQo);
    JSONObject jsonObject = JSON.parseObject(paramStr);
    jsonObject.remove("firstRow");
    jsonObject.remove("currentPage");
    jsonObject.remove("pageSize");
    String countKey = COUNT_PREFIX + jsonObject.toJSONString();
    getCache().remove(LIST_ONE_PREFIX + jsonObject.toString());
  }
}
